package metrics_sql

const (
	K8sNodeMemUsage = `
query = (start=%v,stop=%v,guid="prometheus-%v-0") => {
  node_memory_free_bytes = from(bucket: "data")
    |> range(start: start, stop: stop)
    |> filter(fn: (r) => r["_measurement"] == "prometheus_remote_write")
    |> filter(fn: (r) => r["_field"] == "node_memory_MemFree_bytes")
    |> filter(fn: (r) => r["prometheus_replica"] == guid)
    |> filter(fn: (r) => r["service"] == "node-exporter")
    |> map(fn: (r) => ({_field:"mem_free_bytes",_value:r._value,_time:r._time,instance:r["instance"]}))
    |> window(every: inf)
  node_memory_limit =from(bucket: "data")
    |> range(start: start, stop: stop)
    |> filter(fn: (r) => r["_measurement"] == "prometheus_remote_write")
    |> filter(fn: (r) => r["_field"] == "machine_memory_bytes")
    |> filter(fn: (r) => r["prometheus_replica"] == guid)
    |> filter(fn: (r) => r["service"] == "kubelet")
    |> filter(fn: (r) => r["namespace"] == "kube-system")
    |> filter(fn: (r) => r["metrics_path"] == "/metrics/cadvisor")
    |> last()
    |> map(fn: (r) => ({_field:"node_memory_limit",_value:r._value,_time:r._time,instance:r["node"]}))
    |> window(every: inf)
  return join(tables: {node_memory_free_bytes: node_memory_free_bytes, node_memory_limit: node_memory_limit}, on: [ "_stop", "_start", "instance"])
    |> map(fn: (r) => ({_field:"node_memory_usage",_value: 1.0 - r._value_node_memory_free_bytes /  r._value_node_memory_limit  ,_time:r._time_node_memory_free_bytes,instance:r["instance"],limit: 1}))

}

query()
`
	K8sNodeMemUsagelast = `
query = (start=%v,stop=%v,guid="prometheus-%v-0") => {
  node_memory_free_bytes = from(bucket: "data")
    |> range(start: start, stop: stop)
    |> filter(fn: (r) => r["_measurement"] == "prometheus_remote_write")
    |> filter(fn: (r) => r["_field"] == "node_memory_MemFree_bytes")
    |> filter(fn: (r) => r["prometheus_replica"] == guid)
    |> filter(fn: (r) => r["service"] == "node-exporter")
	|> last()
    |> map(fn: (r) => ({_field:"mem_free_bytes",_value:r._value,_time:r._time,instance:r["instance"]}))
    |> window(every: inf)
  node_memory_limit =from(bucket: "data")
    |> range(start: start, stop: stop)
    |> filter(fn: (r) => r["_measurement"] == "prometheus_remote_write")
    |> filter(fn: (r) => r["_field"] == "machine_memory_bytes")
    |> filter(fn: (r) => r["prometheus_replica"] == guid)
    |> filter(fn: (r) => r["service"] == "kubelet")
    |> filter(fn: (r) => r["namespace"] == "kube-system")
    |> filter(fn: (r) => r["metrics_path"] == "/metrics/cadvisor")
    |> last()
    |> map(fn: (r) => ({_field:"node_memory_limit",_value:r._value,_time:r._time,instance:r["node"]}))
    |> window(every: inf)
  return join(tables: {node_memory_free_bytes: node_memory_free_bytes, node_memory_limit: node_memory_limit}, on: [ "_stop", "_start", "instance"])
    |> map(fn: (r) => ({_field:"node_memory_usage",_value: 1.0 - r._value_node_memory_free_bytes /  r._value_node_memory_limit  ,_time:r._time_node_memory_free_bytes,instance:r["instance"],limit: 1}))
}

query()
`
	GluenetsNodeMemUsage = `
guid = "%v"
job1 = guid+"_glue-cadvisor"
job2 = guid+"_glue-node-exporter"

query = (start=%v,stop=%v) => {
    node_memory_free_bytes = from(bucket: "data")
        |> range(start: start, stop: stop)
        |> filter(fn: (r) => r["_measurement"] == "prometheus_remote_write")
        |> filter(fn: (r) => r["_field"] == "node_memory_MemFree_bytes")
        |> filter(fn: (r) => r["job"] == job2)
        |> map(fn: (r) => ({_field:"mem_free_bytes",_value:r._value,_time:r._time,instance:guid}))
        |> window(every: inf)
    node_memory_limit = from(bucket: "data")
        |> range(start: start, stop: stop)
        |> filter(fn: (r) => r["_measurement"] == "prometheus_remote_write")
        |> filter(fn: (r) => r["_field"] == "machine_memory_bytes")
        |> filter(fn: (r) => r["job"] == job1)
        |> last()
        |> map(fn: (r) => ({_field:"node_memory_limit",_value:r._value,_time:r._time,instance:guid}))
        |> window(every: inf)
    return join(tables: {node_memory_free_bytes: node_memory_free_bytes, node_memory_limit: node_memory_limit}, on: ["_start","_stop","instance"])
        |> map(fn: (r) => ({_field:"node_memory_usage",_value: 1.0 - r._value_node_memory_free_bytes /  r._value_node_memory_limit  ,_time:r._time_node_memory_free_bytes,instance:r["instance"],limit: 1}))

}

query()
`
	GluenetsNodeMemUsagelast = `
guid = "%v"
job1 = guid+"_glue-cadvisor"
job2 = guid+"_glue-node-exporter"

query = (start=%v,stop=%v) => {
    node_memory_free_bytes = from(bucket: "data")
        |> range(start: start, stop: stop)
        |> filter(fn: (r) => r["_measurement"] == "prometheus_remote_write")
        |> filter(fn: (r) => r["_field"] == "node_memory_MemFree_bytes")
        |> filter(fn: (r) => r["job"] == job2)
		|> last()
        |> map(fn: (r) => ({_field:"mem_free_bytes",_value:r._value,_time:r._time,instance:guid}))
        |> window(every: inf)
    node_memory_limit = from(bucket: "data")
        |> range(start: start, stop: stop)
        |> filter(fn: (r) => r["_measurement"] == "prometheus_remote_write")
        |> filter(fn: (r) => r["_field"] == "machine_memory_bytes")
        |> filter(fn: (r) => r["job"] == job1)
        |> last()
        |> map(fn: (r) => ({_field:"node_memory_limit",_value:r._value,_time:r._time,instance:guid}))
        |> window(every: inf)
    return join(tables: {node_memory_free_bytes: node_memory_free_bytes, node_memory_limit: node_memory_limit}, on: ["_start","_stop","instance"])
        |> map(fn: (r) => ({_field:"node_memory_usage",_value: 1.0 - r._value_node_memory_free_bytes /  r._value_node_memory_limit  ,_time:r._time_node_memory_free_bytes,instance:r["instance"],limit: 1}))

}

query()
`
)
